package com.tjetc.view.teacherview;

import cn.hutool.core.date.DateTime;
import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.Examination;
import com.zhiyun.domain.TestPaper;
import com.zhiyun.service.ExaminationService;
import com.zhiyun.service.TestPaperService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In_Exams {
    TestPaperService testPaperService=CilentProxy.getProxyInstance(TestPaperService.class);
    ExaminationService examinationService= CilentProxy.getProxyInstance(ExaminationService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course){
        while (true){
            System.out.println("~~~~~~~~~~~我的考试~~~~~~~~~~");
            System.out.println("1. 查看所有考试");
            System.out.println("2. 发布考试");
            System.out.println("3. 我的题库");
            System.out.println("4. 查看考试详情");
            System.out.println("0. 返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allExams(course);
                    break;
                case 2:
                    createExams(course);
                    break;
                case 3:
                    new MyCourseView_In_Exams_In().start(course);
                    break;
                case 4:
                    MarkExam(course);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入");


            }
        }
    }

    private void MarkExam(Course course) {
        while (true){
            while (true){
                Result examinations = examinationService.getExaminations(course.getId());
                if (examinations.getCode()==200){
                    List<Examination> obj = (List<Examination>) examinations.getObj();
                    System.out.println(examinations.getMsg());
                    for (Examination examination : obj) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                        // 将Date转换为String
                        String start = sdf.format(examination.getStarttime());
                        String end = sdf.format(examination.getEndtime());
                        System.out.println("id:"+examination.getId()+"     考试名称:"+examination.getName()+"     考试简述:"+examination.getDesc()+"     开始时间:"+start+"     结束时间:"+end);
                    }
                    System.out.println("请输入想要查看的考试id");
                    int i = scanner.nextInt();
                    Examination examination=null;
                    for (Examination examination1 : obj) {
                        if (i==examination1.getId()){
                            examination=examination1;
                        }
                    }
                    if (examination!=null){
                        new MyCourseView_In_Exams_MarkExam().start(examination);
                    }
                }else {
                    System.out.println(examinations.getMsg());
                }
                break;
            }
            break;
        }
    }

    private void createExams(Course course) {
        while (true){
            Result result = testPaperService.selectAllTestPaper(AppData.teacher.getId());

            if (result.getCode()==200) {
                List<TestPaper> obj = (List<TestPaper>) result.getObj();
                for (TestPaper testPaper : obj) {
                    System.out.println("题库id"+testPaper.getId()+"   题库名称"+testPaper.getName());
                }
            }else {
                System.out.println(result.getMsg());
                return;
            }
            if (result.getCode()==200){
                System.out.println("请输入想要选择的题库id");
                int i = scanner.nextInt();
                Result result1 = testPaperService.selectByIdWithQuestions(i);
                if (result1.getCode()==200){
                    TestPaper testPaper = (TestPaper) result1.getObj();
                    System.out.println("请输入考试标题");
                    String name = scanner.next();
                    System.out.println("请输入考试描述");
                    String desc = scanner.next();
                    System.out.println("请输入开始时间:");
                    System.out.println("年:");
                    int i0 = scanner.nextInt();
                    System.out.println("月:");
                    int i1 = scanner.nextInt();
                    System.out.println("日:");
                    int i2 = scanner.nextInt();
                    System.out.println("请输入结束时间:");
                    DateTime dateTime = new DateTime(LocalDateTime.of(i0, i1, i2, 0, 0, 0));
                    System.out.println("年:");
                    int j = scanner.nextInt();
                    System.out.println("月:");
                    int j1 = scanner.nextInt();
                    System.out.println("日:");
                    int j2 = scanner.nextInt();
                    DateTime dateTime2 = new DateTime(LocalDateTime.of(j, j1, j2, 0, 0, 0));
                    System.out.println("题目是否乱序(0不乱序  1不乱序)");
                    int i3 = scanner.nextInt();
                    System.out.println("选项是否乱序(0不乱序  1不乱序)");
                    int i4 = scanner.nextInt();
                    Examination examination = new Examination();
                    examination.setName(name);
                    examination.setCourseId(course.getId());
                    examination.setPaperId(testPaper.getId());
                    examination.setStarttime(dateTime);
                    examination.setEndtime(dateTime2);
                    examination.setTitleOrder(i3);
                    examination.setOptionOrder(i4);
                    examination.setDesc(desc);
                    examination.setTestPaper(testPaper);
                    Result result2 = examinationService.addExamination(examination);
                    System.out.println(result2.getMsg());

                }

            }
            System.out.println("");
            break;
        }
    }


    private void allExams(Course course) {
        while (true){
            Result examinations = examinationService.getExaminations(course.getId());
            if (examinations.getCode()==200){
                List<Examination> obj = (List<Examination>) examinations.getObj();
                System.out.println(examinations.getMsg());
                for (Examination examination : obj) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 将Date转换为String
                    String start = sdf.format(examination.getStarttime());
                    String end = sdf.format(examination.getEndtime());
                    System.out.println("id:"+examination.getId()+"     考试名称:"+examination.getName()+"     考试简述:"+examination.getDesc()+"     开始时间:"+start+"     结束时间:"+end);
                }
            }else {
                System.out.println(examinations.getMsg());
            }
            break;
        }
    }
}
